package com.myorg.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CodecPool;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.io.compress.Compressor;
import org.apache.hadoop.util.ReflectionUtils;

public class PooledStreamCompressor {
	public static void main(String[] args) throws Exception {
		String codecClassname = "org.apache.hadoop.io.compress.GzipCodec";
		StringBuffer str = new StringBuffer();
		str.append("Morning Mathew,\n");
		str.append("I thought this would be a good fit for you however if you do not want to be submitted for this opportunity then I understand your decision.\n");
		str.append("Hopefully we can work together on another role in the future.\n");
		str.append("Thanks!");

		Class<?> codecClass = Class.forName(codecClassname);
		Configuration conf = new Configuration();
		CompressionCodec codec = (CompressionCodec) ReflectionUtils
				.newInstance(codecClass, conf);
		Compressor compressor = null;
		try {
			File file = new File("/home/mma/newfile.txt");
			FileOutputStream fop = new FileOutputStream(file);

			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}

			compressor = CodecPool.getCompressor(codec);
			CompressionOutputStream out = codec.createOutputStream(fop,
					compressor);
			// convert String into InputStream
			InputStream is = new ByteArrayInputStream(str.toString().getBytes());
			IOUtils.copyBytes(new BufferedInputStream(is), out, 4096, false);
			out.finish();
			fop.flush();
			fop.close();
			System.out.println("completed!");
		} finally {
			CodecPool.returnCompressor(compressor);
		}
	}
}